import os
import getopt, sys
import getpass

spath = 'log' + os.sep + 'temp' + os.sep
dpath = 'log' + os.sep

def encode():
	psswrd1 = getpass.getpass()
	print "Confirm ",
	psswrd2 = getpass.getpass()
	if psswrd1 != psswrd2:
		print 'The password is not at the same.'
		sys.exit(2)
	
	print 'start encode...'
	for fn in os.listdir(spath):
		if fn.lower().endswith('.jpg') <> True:	
			continue
		print fn
		cmd = 'openssl enc -aes-256-cbc -salt -in ' + spath + fn + ' -out ' + dpath + fn + '.enc' + ' -pass pass:' + psswrd1
		os.system(cmd)

def decode():
	psswrd = getpass.getpass()
	print 'start decode...'
	for fn in os.listdir(dpath):
		if fn.lower().endswith('.enc') <> True:	
			continue
		print fn
		cmd = 'openssl enc -d -aes-256-cbc  -in ' + dpath + fn + ' > ' + spath + fn.replace('.enc', '')  + ' -pass pass:' + psswrd
		os.system(cmd)
	

if __name__ == '__main__':
	try:
		opts, args = getopt.getopt(sys.argv[1:], "de")
	except getopt.GetoptError, err:
		sys.exit(2)
	for opt, arg in opts:
		if opt == '-d':
			decode()
		elif opt == '-e':
			encode()

